package com.m3958.firstgwt.server.servlet;

import java.io.IOException;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.persist.Transactional;
import com.m3958.firstgwt.server.model.PageHitDetail;
import com.m3958.firstgwt.server.model.UrlStatistics;
import com.m3958.firstgwt.server.service.AppUtilService;
import com.m3958.firstgwt.server.service.ReqParaService;
import com.m3958.firstgwt.server.utils.ServerSideStringUtils;
import com.m3958.firstgwt.shared.type.SmartConstants;


@Singleton
public class PageHitDetailServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	@Inject
	private Injector injector;
	
	@Inject
	private AppUtilService autils;
	
	@Inject
	protected com.google.inject.Provider<EntityManager> emp;
	
	
	/*
	 * siteId,referer
	 * 1、不需要任何输出
	 * 2、输出document.write
	 * 3、输出callback
	 */
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) 
	                               throws ServletException, IOException {
		
		ReqParaService reqps = injector.getInstance(ReqParaService.class);
		String askfor = reqps.getStringValue("askfor", "");
		int siteId = reqps.getIdValue("siteid");

		if("sitestat".equals(askfor)){
			
		}else{
			documentWritePageHitCount(req, res, reqps, siteId);
		}
	}

	private void documentWritePageHitCount(HttpServletRequest req,
			HttpServletResponse res, ReqParaService reqps, int siteId) throws IOException {
		
		String referer = reqps.getStringValue("referer", "");
		if(referer.isEmpty()){
			referer = req.getHeader("Referer");
		}
		
		if(siteId == SmartConstants.NONE_EXIST_MODEL_ID || referer == null || referer.isEmpty()){
			return;
		}
		
		Query q = emp.get().createNamedQuery(UrlStatistics.NamedQueries.FIND_URLSTATICS);
		q.setParameter("siteId", siteId);
		q.setParameter("referer", referer);
		UrlStatistics us = null;
		try {
			 us = (UrlStatistics) q.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		Long c = 0L;
		
		if(us == null){
			us = new UrlStatistics();
			us.setCreatedAt(new Date());
			us.setSiteId(siteId);
			us.setReferer(referer);
			createUrlStatistics(us);
		}else{
//			q = emp.get().createNamedQuery(PageHitDetail.NamedQueries.FIND_PAGE_HITCOUNT);
//			q.setParameter("urlstat", us);
//			c = (Long) q.getSingleResult() + 1;
			PageHitDetail phd = new PageHitDetail();
			phd.setUrlstat(us);
			phd.setObname(reqps.getStringValue("obname", ""));
			phd.setObId(reqps.getStringValue("obid", ""));
			phd.setCat1(reqps.getStringValue("cat1", ""));
			phd.setCat2(reqps.getStringValue("cat2", ""));
			phd.setCat3(reqps.getStringValue("cat3", ""));
			phd.setIp(ServerSideStringUtils.ipToInt(req.getRemoteAddr()));
			createPageHitDetail(us,phd);
		}
		autils.writeJsonResponse(res, "document.write('" + us.getHitCount() + "');");
	}
	
	@Transactional
	protected void createPageHitDetail(UrlStatistics us,PageHitDetail phd) {
		emp.get().persist(phd);
		us.setHitCount(us.getHitCount() + 1);
		emp.get().merge(us);
	}

	@Transactional
	protected void createUrlStatistics(UrlStatistics us) {
		emp.get().persist(us);
	}

}
